package com.jie.commonlib.support.permission;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;

import java.util.HashMap;
import java.util.HashSet;

/**
 * 权限对应权限组常量类
 * @author jie
 */
public class PermissionGroup {

    private static final HashMap<String, String> permissionMap = new HashMap<String, String>();

    /**
     * 获取权限对应权限组集合
     */
    public static HashMap<String, String> getPermissionMap(){
        if (permissionMap.isEmpty()){
            permissionMap.put(Manifest.permission.READ_CALENDAR, Manifest.permission_group.CALENDAR);
            permissionMap.put(Manifest.permission.WRITE_CALENDAR , Manifest.permission_group.CALENDAR);
            permissionMap.put(Manifest.permission.READ_CALL_LOG , Manifest.permission_group.CALL_LOG);
            permissionMap.put(Manifest.permission.WRITE_CALL_LOG , Manifest.permission_group.CALL_LOG);
            permissionMap.put(Manifest.permission.PROCESS_OUTGOING_CALLS , Manifest.permission_group.CALL_LOG);
            permissionMap.put(Manifest.permission.CAMERA , Manifest.permission_group.CAMERA);
            permissionMap.put(Manifest.permission.READ_CONTACTS , Manifest.permission_group.CONTACTS);
            permissionMap.put(Manifest.permission.WRITE_CONTACTS , Manifest.permission_group.CONTACTS);
            permissionMap.put(Manifest.permission.GET_ACCOUNTS , Manifest.permission_group.CONTACTS);
            permissionMap.put(Manifest.permission.ACCESS_FINE_LOCATION , Manifest.permission_group.LOCATION);
            permissionMap.put(Manifest.permission.ACCESS_COARSE_LOCATION , Manifest.permission_group.LOCATION);
            permissionMap.put(Manifest.permission.RECORD_AUDIO , Manifest.permission_group.MICROPHONE);
            permissionMap.put(Manifest.permission.READ_PHONE_STATE , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.READ_PHONE_NUMBERS , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.CALL_PHONE , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.ANSWER_PHONE_CALLS , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.ADD_VOICEMAIL , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.USE_SIP , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.ACCEPT_HANDOVER , Manifest.permission_group.PHONE);
            permissionMap.put(Manifest.permission.BODY_SENSORS , Manifest.permission_group.SENSORS);
            permissionMap.put(Manifest.permission.SEND_SMS , Manifest.permission_group.SMS);
            permissionMap.put(Manifest.permission.RECEIVE_SMS , Manifest.permission_group.SMS);
            permissionMap.put(Manifest.permission.READ_SMS , Manifest.permission_group.SMS);
            permissionMap.put(Manifest.permission.RECEIVE_WAP_PUSH , Manifest.permission_group.SMS);
            permissionMap.put(Manifest.permission.RECEIVE_MMS , Manifest.permission_group.SMS);
            permissionMap.put(Manifest.permission.READ_EXTERNAL_STORAGE , Manifest.permission_group.STORAGE);
            permissionMap.put(Manifest.permission.WRITE_EXTERNAL_STORAGE , Manifest.permission_group.STORAGE);
        }
        return permissionMap;
    }

    /**
     * 通过权限组获取对应组名
     * @param group 权限组
     */
    public static String getGroupName(Context context, String group){
        PackageManager packageManager = context.getApplicationContext().getPackageManager();
        try {
            // 获取权限对应权限组名信息
            CharSequence groupName = packageManager.getPermissionGroupInfo(group, 0)
                    .loadLabel(packageManager);
            return groupName.toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取提示信息
     * @param startMsg 提示开头信息
     * @param needRequestList 权限集合
     */
    public static String getTipMsg(Context context, String startMsg, String... needRequestList){
        StringBuilder sb = new StringBuilder();
        sb.append(startMsg);
        HashMap<String, String> permissionMap = PermissionGroup.getPermissionMap();
        // 去重
        HashSet<String> permissionSet = new HashSet<>();
        for (String permission : needRequestList) {
            if (permissionMap.containsKey(permission)) {
                String group = permissionMap.get(permission);
                if (!permissionSet.contains(group)){
                    permissionSet.add(group);
                    String groupName = PermissionGroup.getGroupName(context, group);
                    sb.append("[" + groupName + "]");
                }
            }
        }
        return sb.toString();
    }

}
